package com.chuangyang.fixboxmaster.ui.adapter;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.chuangyang.fixboxmaster.AppData;
import com.chuangyang.fixboxmaster.R;
import com.chuangyang.fixboxmaster.bean.Specification;

import java.util.ArrayList;

/**
 * Created by Administrator on 2015/6/10.
 */
public class SpecificationListAdapter extends BaseAdapter {
    private ArrayList<Specification> mSpecifications = new ArrayList<>();

    private ArrayList<Specification> mSpecificationsTemp = new ArrayList<>();


    public SpecificationListAdapter(ArrayList<Specification> specifications){
        this.mSpecifications=specifications;
    }

    public void setmBrandsTemps(ArrayList<Specification> specifications){
        if (specifications != null)
        {

            for (Specification specification:specifications)
            {
                for (Specification specification1 : mSpecificationsTemp)
                {
                    if (specification.id == specification1.id)
                        break;
                    mSpecificationsTemp.add(specification);
                }
            }
        }
    }

    @Override
    public int getCount() {
        return mSpecifications == null ? 0 : mSpecifications.size();

    }

    @Override
    public Specification getItem(int position) {
        return mSpecifications.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            holder = new ViewHolder();
            convertView = LayoutInflater.from(AppData.getContext()).inflate(R.layout.branch_list_item, parent, false);
            holder.mBrandTitle = (TextView) convertView.findViewById(R.id.list_item_des);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        Specification item = getItem(position);

        if (item != null)
        {
            holder.mBrandTitle.setText(item.title);
        }
        return convertView;
    }


    public static class ViewHolder{
        public TextView mBrandTitle;
    }

    public void filter(String charText) {
        if (charText != null)
        {
            mSpecifications.clear();
            if (charText.length() == 0) {
                mSpecifications.addAll(mSpecificationsTemp);
            } else {
                for (Specification specification : mSpecificationsTemp) {
                    if (specification.title.toLowerCase().contains(charText.toLowerCase())) {
                        mSpecifications.add(specification);
                    }
                }
            }
        }
        notifyDataSetChanged();
    }
}

